﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class InternationalCupFixtures : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.InternationalCupFixtures_PageTitle;
            //if (!HttpContext.Current.User.Identity.IsAuthenticated)
            //    Response.Redirect("Default.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            
            //if (ou.TeamId == "Null")
            //    Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            string Season = "-1";
            string Round = "0";
            if (!IsPostBack)
            {
                if (Request.QueryString["season"] != null)
                    Season = Request.QueryString["season"].ToString();
                if (Request.QueryString["round"] != null)
                    Round = Request.QueryString["round"].ToString();
                if (Season == "-1")
                {
                    Season = (GND.ReadNivdalIntData("Nivdal_GetCurrentInternationalCupSeason"));
                }
                SeasonHiddenField.Value = Season;
                if (Round == "0")
                    Round = (GND.ReadNivdalIntData("@Season", "Nivdal_GetCurrentInternationalCupRound", Season));

                LanguageHiddenField.Value = ou.Language;

                this.InternationalCupFixturesSqlDataSource.SelectParameters.Add("Season", Season);
                this.InternationalCupFixturesSqlDataSource.SelectParameters.Add("Round", Round);
                this.InternationalCupFixturesSqlDataSource.SelectParameters.Add("Language", ou.Language);
                this.RoundSqlDataSource.SelectParameters.Add("Season", Season);
                this.RoundSqlDataSource.SelectParameters.Add("Language", ou.Language);

                this.InternationalCupLinkButton.PostBackUrl = "InternationalCup.aspx?season=" + Season;
                this.SeasonTitleLabel.Text = Resources.Resource.InternationalCupFixtures_SeasonTitleLabelText + " " + Season + " " +
                                             Resources.Resource.Global_Round + ": " + Round;

                if (ou.TeamId == null)
                    AddToLiveAllPanel.Visible = false;
            }


        }

        protected void RoundDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.InternationalCupFixturesSqlDataSource.SelectParameters.Clear();
            this.InternationalCupFixturesSqlDataSource.SelectParameters.Add("Round", RoundDropDownList.SelectedValue);
            this.InternationalCupFixturesSqlDataSource.SelectParameters.Add("Season", SeasonHiddenField.Value);
            this.InternationalCupFixturesSqlDataSource.SelectParameters.Add("Language", LanguageHiddenField.Value);
            this.RoundSqlDataSource.SelectParameters.Clear();
            this.RoundSqlDataSource.SelectParameters.Add("Season", SeasonHiddenField.Value);
            this.RoundSqlDataSource.SelectParameters.Add("Language", LanguageHiddenField.Value);

            this.SeasonTitleLabel.Text =
                    Resources.Resource.InternationalCupFixtures_SeasonTitleLabelText + " " + SeasonHiddenField.Value + " " +
                    Resources.Resource.Global_Round + ": " + RoundDropDownList.SelectedValue;
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected void AddToLive(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "AddToLive")
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                OnlineUser ou = (OnlineUser)Session["User"];
                GND.ExecNivdalSQL("UserId", "MatchId", "Nivdal_LiveAddMatchToLive", ou.UserId, e.CommandArgument);
                //Response.Redirect("Live.aspx");
                (Master.Master.FindControl("LiveListView") as ListView).DataBind();
            }
        }

        protected void AddSelectedToLive(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string MatchId;
            bool MatchAdded = false;
            foreach (GridViewRow r in GridView1.Rows)
            {
                if (r.RowType == DataControlRowType.DataRow)
                {
                    try
                    {
                        MatchId = (r.FindControl("MatchIdHiddenField") as HiddenField).Value;
                        if ((r.FindControl("AddLiveCheckBox") as CheckBox).Checked)
                        {
                            GND.ExecNivdalSQL("UserId", "MatchId", "Nivdal_LiveAddMatchToLive", ou.UserId, MatchId);
                            MatchAdded = true;
                            (r.FindControl("AddLiveCheckBox") as CheckBox).Checked = false;
                        }
                    }
                    catch { }
                }
            }
            if (MatchAdded)
            {
                try
                {
                    ((Master.Master.FindControl("LoginView1") as LoginView).FindControl("LiveListView") as Repeater).Visible = true;
                    ((Master.Master.FindControl("LoginView1") as LoginView).FindControl("LiveListView") as Repeater).DataBind();
                    ((Master.Master.FindControl("LoginView1") as LoginView).FindControl("LiveUpdatePanel") as UpdatePanel).Update();
                }
                catch { }
            }
        }
    }
}